//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

import { mapGetters, mapActions } from "vuex";
import template from "./withdrawTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  data() {
    return {
      bankCardMsg: {},
      money: "",
      psd: "",
      psdShow: true,
      isBindBank: false,
      isSafe: false,

      limit: "", //打码量
      limitFlag: true,
    };
  },
  computed: {
    ...mapGetters(["accountData"]),
  },
  created() {
    if (this.accountData.is_banded_bank == 0) {
      this.isBindBank = true;
    } else if (this.accountData.is_set_paypassword == 0) {
      this.isSafe = true;
    } else {
      this._getBankCard();
    }
    this._getNeedBet();
  },
  methods: {
    ...mapActions(["SET_ACCOUNT_DATA"]),
    handleInput(e) {
      this.money = e.target.value.replace(/[^\d]/g, "").replace(/^0{1,}/g, "");
    },
    nextStep() {
      if (this.money == "") {
        this.$vux.toast.show({
          text: "提现金额不能为空",
        });
      } else if (Number(this.money) < Number(this.bankCardMsg.lower_limit)) {
        this.$vux.toast.show({
          text: "提现金额不能小于最低提现金额",
        });
      } else if (Number(this.bankCardMsg.upper_limit) < Number(this.money)) {
        this.$vux.toast.show({
          text: "提现金额不能大于最高提现金额",
        });
      } else {
        this.psdShow = false;
        this.psd = "";
        this.$nextTick(() => {
          this.$refs.psd.focus();
        });
      }
    },
    _getNeedBet() {
      let vm = this;
      let params = {};
      params.id = this.accountData.user_id;
      params.token = localStorage.getItem("token");
      vm.$http
        .post(vm.urlRequest + "?m=api&c=app&a=getNeedBet", params)
        .then((res) => {
          if (res.status == 0) {
            vm.limit = res.limit;
            if (vm.limit > 0) {
              vm.limitFlag = true;
            } else {
              vm.limitFlag = false;
            }
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    _getBankCard() {
      let vm = this;
      let params = {};
      params.token = localStorage.getItem("token");
      vm.$vux.loading.show();
      vm.$http
        .post(vm.urlRequest + "?m=api&c=cash&a=getBankCard", params)
        .then((res) => {
          if (res.status == 0) {
            vm.bankCardMsg = res;
          }
          vm.$vux.loading.hide();
        })
        .catch((err) => {
          vm.$vux.loading.hide();
          console.log(err);
        });
    },
    _submidtCash() {
      let vm = this;
      if (
        vm.bankCardMsg.withdraw_limit.withdrwlFee > 0 &&
        vm.bankCardMsg.withdraw_limit.freeCont <=
          vm.bankCardMsg.withdraw_limit.withdrwlCont
      ) {
        vm.$vux.confirm.show({
          content:
            "您今天提现次数已经超出系统设置的每天免费提现次数限制，继续提现需要额外手续费:" +
            vm.bankCardMsg.withdraw_limit.withdrwlFee +
            "元/次",
          onCancel() {
            vm.$router.push("/wallet");
          },
          onConfirm() {
            vm._goWithdraw();
          },
        });
      } else {
        vm._goWithdraw();
      }
    },
    _goWithdraw() {
      let vm = this;
      let params = {};
      params.token = localStorage.getItem("token");
      params.bank_id = vm.bankCardMsg.bank_id;
      params.money = vm.money;
      params.psd = vm.psd;
      vm.$vux.loading.show();
      vm.$http
        .post(vm.urlRequest + "?m=api&c=cash&a=cash", params)
        .then((res) => {
          if (res.status == 0) {
            let userAccountData = vm.accountData;
            userAccountData.cash_id = res.cash_id;
            vm.SET_ACCOUNT_DATA({
              Obj: userAccountData,
            });
            vm.$router.push("/whdProgress");
            vm.$router.push({
              path: "/whdProgress",
              query: { cash_id: res.cash_id },
            });
          } else {
            // vm.$vux.toast.text(res.ret_msg)
            vm.psdShow = true;
            if (res.ret_msg && res.ret_msg != "") {
              vm.$vux.toast.show({
                text: res.ret_msg,
              });
            }
          }
          vm.$vux.loading.hide();
        })
        .catch((err) => {
          vm.$vux.loading.hide();
          console.log(err);
        });
    },
  },
  watch: {
    accountData(obj) {
      if (obj) {
        if (obj.is_banded_bank == 0) {
          this.isBindBank = true;
        } else if (obj.is_set_paypassword == 0) {
          this.isSafe = true;
        } else {
          this._getBankCard();
        }
        this._getNeedBet();
      }
    },
    // money(newVal, oldVal) {
    //     if (newVal.charAt(0) == 0 && newVal.length > 1) {
    //         this.money = oldVal
    //     }
    // },
    psd() {
      // console.log(this.psd)
      // this.psd = this.psd.trim()
      // if(isNaN(this.psd)){
      //     this.psd = this.psd.slice(0, this.psd.length -1)
      // }
      let spans = document.querySelectorAll(".inp span");
      for (let i = 0; i < spans.length; i++) {
        spans[i].style.backgroundSize = "0";
      }
      for (let i = 0; i < this.psd.length; i++) {
        spans[i].style.backgroundSize = "auto";
      }
      if (this.psd.length == 6) {
        this._submidtCash();
      }
    },
  },
};

// WEBPACK FOOTER //
// ./src/components/wallet/withdraw/withdraw.vue
